package cn.com.template.controller.system.admin;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.commons.Paginator;
import cn.com.commons.utils.StringUtil;
import cn.com.dal.dataobject.hr.DeptDO;
import cn.com.service.hr.DeptService;
import cn.com.support.context.UserContext;
import cn.com.support.message.MessageResult;
import cn.com.template.ui.Flexigrid;
import cn.com.template.utils.FlexigridUtil;

/**
 * 
 * 部门控制器
 * 
 * @since 1.0 2012-2-21 下午11:03:53
 * @version 1.0
 * @author Zoe
 */
@Controller
@RequestMapping("/system/admin/branchmanager")
public class DeptManagerController {

	@Autowired
	private DeptService deptService;
	
	@RequestMapping
	public ModelAndView forwardBranchManager() {
		ModelAndView mav = new ModelAndView("system/admin/branchmanager");
		return mav;
	}
	
	@RequestMapping("/getDeptInfo")
	@ResponseBody
	public Flexigrid getDeptInfo(HttpServletRequest request, HttpServletResponse response){
		Flexigrid flexigrid = null;
		try {
			Paginator<DeptDO, List<DeptDO>> paginator = new Paginator<DeptDO, List<DeptDO>>();
			//deptname
			String name = request.getParameter("deptname");
			DeptDO deptDO = new DeptDO();
			if(StringUtil.isNotBlank(name)){
				deptDO.setName(name);
			}
			paginator.setCondition(deptDO);
			// 设置分页器
			paginator.setPageSize(Integer.parseInt(request.getParameter("rp")));
			paginator.setCurrentPage(Integer.parseInt(request.getParameter("page")));
			deptService.selectByPage(paginator);
			//PACKAGE FLEXIGRID DATA
			flexigrid = FlexigridUtil.formatFlexigridData(paginator);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flexigrid;
	}

	@RequestMapping("/addDeptInfo")
	@ResponseBody
	public MessageResult<Long> addDeptInfo(HttpServletRequest request, HttpServletResponse response){
		MessageResult<Long> result = null;
		try {
			String deptName = request.getParameter("deptname");
			String deptDesc = request.getParameter("deptdesc");
			DeptDO deptDO = new DeptDO();
			deptDO.setName(deptName.trim());
			deptDO.setDescription(deptDesc.trim());
			deptDO.setCreator(UserContext.getUsername());
			result = deptService.insert(deptDO);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@RequestMapping("/updateDeptInfo")
	@ResponseBody
	public MessageResult<Integer> updateDeptInfo(HttpServletRequest request, HttpServletResponse response){
		MessageResult<Integer> result = null;
		try {
			String deptName = request.getParameter("deptname");
			String deptDesc = request.getParameter("deptdesc");
			String id = request.getParameter("deptid");
			DeptDO deptDO = new DeptDO();
			deptDO.setId(Long.parseLong(id.trim()));
			deptDO.setName(deptName.trim());
			deptDO.setDescription(deptDesc.trim());
			deptDO.setModifier(UserContext.getUsername());
			result = deptService.updateById(deptDO);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@RequestMapping("/delDeptInfo")
	@ResponseBody
	public MessageResult<Integer> delDeptInfo(HttpServletRequest request, HttpServletResponse response){
		MessageResult<Integer> result = null;
		try {
			String idStrs = request.getParameter("ids");
			result = deptService.deleteById(Arrays.asList(idStrs.split(",")));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
}
